package com.intelligentcar.springboot.service.serverImpl;

import com.intelligentcar.springboot.bean.Menu;
import com.intelligentcar.springboot.bean.Role;
import com.intelligentcar.springboot.mapper.MenuMapper;
import com.intelligentcar.springboot.service.MenuServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class MenuServerImpl implements MenuServer {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Role> selectAllRole() {
        return menuMapper.selectAllRole();
    }

    @Override
    public List<Menu> SelectAllotMenu1(Menu menu) {
        return menuMapper.selectAllotMenu(menu);
    }

    @Override
    public List<Integer> SelectAllotMenu(Menu menu) {
        List<Integer> list = new ArrayList<>();
        List<Menu> list1 = menuMapper.selectAllotMenu(menu);
        for (Menu menu1:list1) {
            list.add((int) menu1.getMenuId());
        }
        return list;
    }

    @Override
    public List<Menu> selectAllPower() {
        return menuMapper.selectAllPower();
    }

    @Override
    public boolean updatePower(Menu menu) {
        for (int i=0; i<menu.getAllotPowerlist().size(); i++){
            menuMapper.updatePower(menu.getMenuState(),menu.getRoleId(), menu.getAllotPowerlist().get(i));
        }
        return true;
    }

    @Override
    public Map<String,Object> menuTable(Menu menu) {
        menu.setPage((menu.getPage()-1) * menu.getLimit());
        Map<String,Object> map = new HashMap<>();
        map.put("menuList",menuMapper.menuTable(menu));
        map.put("count",menuMapper.selectPageCount());
        return map;
    }

    @Override
    public boolean updateMenu(Menu menu) {
        int i = menuMapper.updateMenu(menu);
        if (i>0) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean updateMenuState(Menu menu) {
        int i = menuMapper.updateMenuState(menu);
        if (i > 0) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public int insertMenu(Menu menu) {
        String time =new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss").format(new Date());
        menu.setAddTime(time);
        String router[] = menu.getMenuUrl().split("/");
        if (router.length < 2){
           return 2;
        }
        menu.setRouterName(router[router.length - 1]);
        int i = menuMapper.insertMenu(menu);
        if (i>0){
            Menu menu1 = menuMapper.selectMenuId(menu);
            List<Role> roles = menuMapper.selectAllRole();
            List<Menu> menus = new ArrayList<>();
            for (Role role: roles) {
                System.out.println(role.getRoleId() + "," + menu1.getMenuId() + "9999");
                Menu menu2 = new Menu();
                menu2.setAddTime(time);
                menu2.setMenuId(menu1.getMenuId());
                menu2.setRoleId((int) role.getRoleId());
                menus.add(menu2);
            }
            int j = menuMapper.insertRolePower(menus);
            if (i > 0 && j > 0) {
                return 1;
            }
        }
        return 0;
    }

    @Override
    public Map<String, Object> selectMenu(Menu menu) {
        menu.setPage((menu.getPage()-1) * menu.getLimit());
        if (menu.getMenuName() != "" && menu.getMenuName() != null){
            menu.setMenuName("%"+menu.getMenuName()+"%");
        }
        Map<String,Object> map = new HashMap<>();
        List<Menu> list = menuMapper.selectMenu(menu);
        map.put("menuList",list);
        map.put("count",menuMapper.selectedPageCount(menu));
        return map;
    }

    @Override
    public List<Menu> allParentMenu(Menu menu) {
        return menuMapper.allParentMenu(menu);
    }

    @Override
    public List<Menu> routerData() {
        List<Menu> list = menuMapper.routerData();
        return list;
    }
}
